﻿using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Data.MainServiceData;
using CPPEI.Coolzon.Server.CacheData;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.API.Handlers
{
    class SetPropertyXmlStringHandler : CPPEI.Net.Original.IPackageHandler
    {
        public Net.Original.Package Handle(Net.Original.Package package)
        {
            DateTime startTime = DateTime.Now;
            int offset = Net.Original.Package.HeaderLength;

            string strDomain = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset, 128);

            string solution = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset, 48);
            string scene = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset, 48);
            string part = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset, 48);
            string property = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset, 48);
            string xmlString = CPPEI.Net.BufferUtil.StringFromBuffer(package.Buffer, ref offset);

            Common.Logger.DebugToTag("ApiService", "SetPropertyXmlStringHandler domain:{0}, solution:{1}, scene:{2}, part:{3}, property:{4}, xmlstring:{5}", strDomain, solution, scene, part, property, xmlString);

            RespSetPropertyXmlStringData responseData = new RespSetPropertyXmlStringData();
            responseData.SceneName = scene;
            responseData.PartName = part;
            responseData.PropertyName = property;
            responseData.XmlString = xmlString;

            Response response = new Response();
            if (responseData != null)
            {
                response.Data = CPPEI.Coolzon.Common.SerializeUtility.SerializeToBinary(responseData);
            }
            long handleTicks = (DateTime.Now - startTime).Ticks;
            ResponseHeader responseHeader = new ResponseHeader();
            responseHeader.Scenario = RESPONSE_SCENARIO.SCENE;
            responseHeader.Command =RESPONSE_COMMAND.SCENE.SETPROPERTYXMLSTRING;
            responseHeader.HandleTicks = handleTicks / 10000;
            if (response.Data != null)
            {
                responseHeader.DataLength = response.Data.Length;
            }
            responseHeader.SendTicks = 0;
            responseHeader.RequestHeader = null;

            response.Header = responseHeader;

            CacheData.ControlDomain domain = ServerData.Instance.GetDomain(strDomain);

            if (domain != null)
            {
                List<CacheData.Client> clientList = domain.GetClientList();

                // 通知各个客户端场景事件
                for (int index = 0; index < clientList.Count; index++)
                {
                    CacheData.Client client = clientList[index];
                    if (client.Callback != null
                        && client.State == ClientState.InDomain)
                    {
                        try
                        {
                            client.Callback.OnReceiveResponse(response);
                        }
                        catch (Exception ex)
                        {
                            Common.Logger.ErrorToTag("ApiService", "发送给客户端:{0},出错,ex:{1}", client.UserIdentity, ex);
                            client.Callback = null;
                        }
                    }
                }
            }
            else
            {
                Common.Logger.ErrorToTag("ApiService", "未发现域:{0}", strDomain);
                            
            }
            return null;
        }
    }
}
